package insat.challenge.metier;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;



import android.util.Log;

public class PlaceCategory {

	private String name;
	private String id;
	private Vector<PlaceCategory> categories = new Vector<PlaceCategory>();

	public Vector<PlaceCategory> getCategories() {

		// PlaceCategory placeCategory = new PlaceCategory();
		// placeCategory.setName("Hopitaux");
		//
		//
		// categories.add(placeCategory);
		// PlaceCategory placeCategory1 = new PlaceCategory();
		// placeCategory1.setName("Cliniques");
		// categories.add(placeCategory1);
		// PlaceCategory placeCategory2 = new PlaceCategory();
		// placeCategory2.setName("Pharmacies");
		// categories.add(placeCategory2);


		String readboxHelpFeed = readboxHelpFeed();
		try {
			JSONObject objet = new JSONObject(readboxHelpFeed);
			JSONArray jsonArray = new JSONArray();
			jsonArray = objet.getJSONArray("placescategories");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				PlaceCategory placeCategory = new PlaceCategory();
				placeCategory.setName(jsonObject.getString("name"));
				placeCategory.setId(jsonObject.getString("id"));
				Log.i("Objetttt", jsonObject.getString("name"));
				Log.i("Objetttt _ id", jsonObject.getString("id"));
				categories.add(placeCategory);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//

		return categories;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setCategories(Vector<PlaceCategory> categories) {

		this.categories = categories;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	PlaceCategory getItem(int location) {
		return categories.get(location);
	}

	public String toString() {
		return name;
	}

	public String readboxHelpFeed() {
		StringBuilder builder = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(
				"http://helpbox.belhassineamine.com/index.php/api/getallplacescategories.json");
		try {
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200) {
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(content));
				String line;
				while ((line = reader.readLine()) != null) {
					builder.append(line);
				}
			} else {
				
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return builder.toString();
	}

}
